<template>
  <div class="my-container">
    <!-- 已登录的盒子 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image fit="cover" :src="userInfo.photo" class="avartar" round />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round type="info" size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="data-status">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录的盒子 -->
    <div class="header not-login" v-else>
      <div class="login-btn">
        <img
          class="mobile-img"
          src="~@/assets/mobile.png"
          alt=""
          @click="$router.push('/login')"
        />
        <span class="text">登录/注册</span>
      </div>
    </div>

    <!-- 宫格导航 -->
    <van-grid :column-num="2" class="gird-nav" clickable>
      <van-grid-item class="gird-item">
        <template #icon>
          <i class="toutiao toutiaoshoucang"></i>
        </template>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="gird-item">
        <i slot="icon" class="toutiao toutiaolishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- cell单元格 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      title="退出登录"
      class="login_out"
      v-if="user"
      clickable
      @click="onLoginOut"
    />
  </div>
</template>

<script>
// 从容器中将存储token的数据导入
import { mapState } from 'vuex';
// 将请求方法导入
import { getUserInfo } from '@/api/user.js';
export default {
  name: 'MyPage',
  data() {
    return {
      // 存储用户信息
      userInfo: {},
    };
  },
  methods: {
    // 退出登录
    onLoginOut() {
      // 在组件中使用this.$dialog来调用dialog弹窗组件
      this.$dialog
        .confirm({
          title: '您确定要退出登录？',
        })
        .then(() => {
          // console.log('我退出登录了');
          // 清除容器的的user和本地存储中的token
          // 调用mutations里面的方法来操作state的数据
          // 将数据的user赋值为null
          this.$store.commit('setUser', null);
        })
        .catch(() => {
          console.log('我取消了退出登录');
        });
    },
    // 调用方法，发送请求获取用户信息
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo();
        // console.log(data);
        this.userInfo = data.data;
        // this.$toast('获取数据成功！');
      } catch (err) {
        this.$toast('获取数据失败，请稍后重试！');
        console.log('获取数据失败，请稍后重试！', err);
      }
    },
  },
  created() {
    // 如果用户登录了，才来加载用户信息
    if (this.user) {
      this.loadUserInfo();
    }
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

<style lang="less" scoped>
.my-container {
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
    .login-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      background: url('~@/assets/banner.png');
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avartar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-status {
      height: 130px;
      display: flex;
      background: url('~@/assets/banner.png');
      .data-item {
        height: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .count {
          color: #fff;
          font-size: 36px;
        }
        .text {
          color: #fff;
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 25px;
      }
      .toutiaoshoucang {
        color: #eb5253;
      }
      .toutiaolishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .login_out {
    text-align: center;
  }
}
</style>
